﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.DocumentTypes;
using Vega.USiteBuilder;
using SPCFMedicineInformer.Members;
using System.Web.Security;

using umbraco.MacroEngines;
using umbraco.NodeFactory;
using umbraco;
using Fonqi.Umbraco;
using SPCFMedicineInformer.Constants;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Content_AbbreviationIndex : System.Web.UI.MasterPage
    {
        public int PageNum { get; set; }
        public int TotalRec { get; set; }
        int limitPerPage = 20;
        string chr = "";
        static List<string> realLetters = new string[] {
                "a"
                ,"b"
                ,"c"
                ,"d"
                ,"e"
                ,"f"
                ,"g"
                ,"h"
                ,"i"
                ,"j"
                ,"k"
                ,"l"
                ,"m"
                ,"n"
                ,"o"
                ,"p"
                ,"q"
                ,"r"
                ,"s"
                ,"t"
                ,"u"
                ,"v"
                ,"w"
                ,"x"
                ,"y"
                ,"z" }.ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            chr = Request.QueryString["chr"] != null ? Request.QueryString["chr"].ToString().ToCharArray().FirstOrDefault().ToString().ToLower() : "";
            if (!IsPostBack)
            {
                string strpage = Request.QueryString["p"] != null ? Request.QueryString["p"].ToString() : "1";
                int tempInt;
                PageNum = int.TryParse(strpage, out tempInt) ? tempInt : 1;
                
                BindGrid();
                
            }

        }

        void BindGrid()
        {
            bool isLetter = realLetters.Contains(chr.FirstOrDefault().ToString()) || chr == "";
            var list = NodeLocator.GetNodesOfExactType<AbbreviationItem>().Where(a => 
                                                                                (a.Abbreviation.ToLower().TrimStart().StartsWith(chr) && isLetter) ||
                                                                                (!realLetters.Contains(a.Abbreviation.FirstOrDefault().ToString().ToLower()) && !isLetter))
                                                                          .OrderBy(a => a.Id)
                                                                          .ToList();
            TotalRec = list.Count();
            list = list.Skip((PageNum - 1) * limitPerPage).Take(limitPerPage).ToList();
            DisplayPaging();
            RepMedicineItem.DataSource = list;
            RepMedicineItem.DataBind();
        }
        void DisplayPaging()
        {
            int maxPage = (TotalRec / limitPerPage) + (TotalRec % limitPerPage > 0 ? 1 : 0);
            List<string> pageList = new List<string>();
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString());
            }

            rptPaging.DataSource = pageList;
            rptPaging.DataBind();

        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "paging":
                        PageNum = intTemp;
                        BindGrid();
                        break;
                }
            }
        }
        
        protected void Repeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = (LinkButton)e.Item.FindControl("linkButPaging");
                Literal lit = (Literal)e.Item.FindControl("litPaging");
                if (PageNum.ToString() == lit.Text)
                {
                    lb.CssClass = "current";
                }
            }

        }
        
    }
}